<?php

/**
 * @file IndexController
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Report_IndexController extends Zend_Controller_Action {

    // Filtrar: Ejecutivo, Creativo, Clientes
    // Filtro de fechas
    // Nro de orden entregadas

    /* Report main all agency */
    public function indexAction() {
        
    }

    public function getReportAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $ordenDb = new Orden_Model_DbTable_Orden();

        $select = $ordenDb->select()->setIntegrityCheck(false)
                ->from(array('od' => 'orden_departament'), array('id_departament'))
                ->join(array('o' => 'orden'), 'o.id = od.id_orden', array('nro_ordenes' => 'COUNT(*)'))->where('o.status != 4')
                ->join(array('d' => 'departament'), 'd.id = od.id_departament')
                ->group('od.id_departament');

        $dataOr = $ordenDb->fetchAll($select);
        $jsonData = Zend_Json::encode($dataOr->toArray());
        echo $jsonData;
        exit;
    }

    public function getReportCustomAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        if ($this->_request->isXmlHttpRequest()) {
            $ordenDb = new Orden_Model_DbTable_Orden();
            
            $data = $this->_request->getParams();
            
            $originalFromDate = $data['from_date'];
            $newFromDate = date("Y-m-d", strtotime($originalFromDate));
            $originalToDate = $data['to_date'];
            $newToDate = date("Y-m-d", strtotime($originalToDate));
            
            if ($data['departament'] != 0) {
                $select = $ordenDb->select()->setIntegrityCheck(false)
                        ->from(array('od' => 'orden_departament'), array('id_departament'))->where('od.id_departament=?', $data['departament'])
                        ->join(array('o' => 'orden'), 'o.id = od.id_orden', array('nro_ordenes' => 'COUNT(*)'))
                        ->where('o.create_date > ?', $newFromDate)->where('o.create_date < ?', $newToDate)
                        ->join(array('d' => 'departament'), 'd.id = od.id_departament')
                        ->group('od.id_departament');
            }else{
                $select = $ordenDb->select()->setIntegrityCheck(false)
                        ->from(array('od' => 'orden_departament'), array('id_departament'))
                        ->join(array('o' => 'orden'), 'o.id = od.id_orden', array('nro_ordenes' => 'COUNT(*)'))
                        ->where('o.create_date > ?', $newFromDate)->where('o.create_date < ?', $newToDate)
                        ->join(array('d' => 'departament'), 'd.id = od.id_departament')
                        ->group('od.id_departament');
            }

            $dataOr = $ordenDb->fetchAll($select);
            $jsonData = Zend_Json::encode($dataOr->toArray());
            echo $jsonData;
            exit;
        }
    }

}

?>
